package org.example.test;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.poizon.infrastructure.idem.config.change.listener.IdemDbStoreConfigChangeStrategy;
import com.poizon.infrastructure.idem.core.domain.value.config.IdemConstants;
import com.poizon.infrastructure.idem.core.domain.value.enums.IdemDbStoreType;
import com.poizon.infrastructure.idem.core.recovery.IdemDbStoreFailover;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.testng.annotations.Test;

import java.util.Properties;

public class ConfigChangeListenerTest {
    @Test
    public void coolRequest配置变更测试() throws Exception {
        IdemDbStoreConfigChangeStrategy idemDbStoreConfigChangeStrategy = new IdemDbStoreConfigChangeStrategy();
        Assert.isNull(idemDbStoreConfigChangeStrategy.getChangedProperty(null));
        Properties properties = new Properties();
        Assert.isTrue(StrUtil.isBlank(idemDbStoreConfigChangeStrategy.getChangedProperty(properties)));
        properties.setProperty(
                IdemConstants.DB_STORE_TYPE_CONFIG,
                IdemDbStoreType.redis.getDbStoreType()
        );
        Assert.equals(IdemDbStoreType.redis.getDbStoreType(), idemDbStoreConfigChangeStrategy.getChangedProperty(properties));

        Assert.isTrue(idemDbStoreConfigChangeStrategy.support(properties));
        properties.setProperty(
                IdemConstants.DB_STORE_TYPE_CONFIG,
                ""
        );
        Assert.isFalse(idemDbStoreConfigChangeStrategy.support(properties));

        idemDbStoreConfigChangeStrategy.configChange(null);
        idemDbStoreConfigChangeStrategy.configChange(new Properties());
        Properties properties1 = new Properties();
        properties1.setProperty(
                IdemConstants.DB_STORE_TYPE_CONFIG,
                IdemDbStoreType.redis.getDbStoreType()
        );
        idemDbStoreConfigChangeStrategy.configChange(properties1);
        properties1.setProperty(
                IdemConstants.DB_STORE_TYPE_CONFIG,
                IdemDbStoreType.mongodb.getDbStoreType()
        );
        MockedStatic<IdemDbStoreFailover> idemDbStoreFailoverMockedStatic = Mockito.mockStatic(IdemDbStoreFailover.class);
        idemDbStoreFailoverMockedStatic.when(() -> {
            IdemDbStoreFailover.changeIdemDbStoreInstantly(IdemDbStoreType.mongodb.getDbStoreType());
        }).thenReturn(true);
        IdemDbStoreFailover.changeIdemDbStoreInstantly(IdemDbStoreType.mongodb.getDbStoreType());
        idemDbStoreConfigChangeStrategy.configChange(properties1);
    }
}
